package com.eclipse.training.extensions.handlers;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import com.eclipse.training.extensions.LogManager;
import com.eclipse.training.extensions.builder.TrainingNature;

/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * 
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class CommandHandler extends AbstractHandler {
	/**
	 * The constructor.
	 */
	public CommandHandler() {
	}

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil
				.getActiveWorkbenchWindowChecked(event);

		if (MessageDialog.openQuestion(window.getShell(), Messages.CommandHandler_0,
				Messages.CommandHandler_1)) {

			Job job = new Job(Messages.CommandHandler_2) {

				@Override
				protected IStatus run(IProgressMonitor monitor) {
					ArrayList<IProject> projects = getWorkspaceProjects();
					monitor.beginTask(Messages.CommandHandler_3,
							projects.size());
					
					for (IProject project : projects) {
						monitor.subTask(Messages.CommandHandler_4 + project.getName());

						if (monitor.isCanceled()) {
							monitor.done();
							return Status.CANCEL_STATUS;
						}

						try {
							project.build(IncrementalProjectBuilder.FULL_BUILD,
									new SubProgressMonitor(monitor, 1));

							TimeUnit.SECONDS.sleep(5);

							LogManager.getLogger().logInfo(
									Messages.CommandHandler_5 + project
											+ Messages.CommandHandler_6);
						} catch (CoreException e) {
							LogManager.getLogger().logError(
									Messages.CommandHandler_7
											+ project, e);
						} catch (InterruptedException e) {
							LogManager.getLogger().logError(e);
						}

						monitor.worked(1);
					}

					monitor.done();

					return Status.OK_STATUS;
				}
			};
			job.setUser(true);
			job.schedule();
		}

		return null;
	}

	protected ArrayList<IProject> getWorkspaceProjects() {
		ArrayList<IProject> projects = new ArrayList<IProject>();

		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

		for (IProject project : root.getProjects()) {
			try {
				if (project.hasNature(TrainingNature.NATURE_ID))
					projects.add(project);
			} catch (CoreException e) {
				LogManager.getLogger().logError(e);
			}
		}

		return projects;
	}

}
